package com.fanfanwang.ffw.app.ui.my;

import java.io.File;
import java.util.ArrayList;
import java.util.MissingResourceException;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Vibrator;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.fanfanwang.ffw.app.AppManager;
import com.fanfanwang.ffw.app.R;
import com.fanfanwang.ffw.app.adapter.ZhiHangListAdapter;
import com.fanfanwang.ffw.app.asynctask.GetYuyueListAsyncTask;
import com.fanfanwang.ffw.app.bean.ServiceBean;
import com.fanfanwang.ffw.app.dialog.MyRemindTimeDialog;
import com.fanfanwang.ffw.app.receiver.OneShotAlarm;
import com.fanfanwang.ffw.app.tools.SharedPreferencesUtil;
import com.fanfanwang.ffw.app.widget.CommonTitleBar;
import com.fanfanwang.ffw.app.widget.SwitchButton;
import com.fanfanwang.ffw.app.widget.SwitchButton.OnChangeListener;

/**
 * 消息设置
 * @author lyf
 *
 */
public class MsgSetAct extends com.fanfanwang.ffw.app.base.BaseActivity {

	private CommonTitleBar commonTitleBar;
	private Activity mAct;
	private Intent mIntent;
	private ToggleButton switch_message_tongzhi,switchbutton_receive_shake,switchbutton_shake,switch_receive_alarm_clock;
	private LinearLayout ll_message_tongzhi;
	private LinearLayout ll_receive_alarm_clock;
	private Vibrator vibrator;
	private RelativeLayout rl_remindtime;//提醒时间的列
	private RelativeLayout rl_sound;//铃声
	private TextView tv_remindtime;//显示提醒的时间
	private TextView tv_remind_sound;//显示提醒的声音
	private TextView alarm_msg_hint;//闹钟提醒的提示信息

	public  static IRemindTimeable iRemindTimeable;
	public  static IRemindSoundable iRemindSoundable;
	SharedPreferences sp;
	private String ringSound="";
	private int pos=0;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initWidget();
		initData();
		AppManager.getAppManager().addActivity(this);
		widgetClick();
	}

	public void initWidget() {
		setContentView(R.layout.activity_my_msg_set);
		mAct = this;
		/*初始化SharedPreferences*/
		sp = getSharedPreferences("ring", Context.MODE_PRIVATE);
		vibrator = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);  
		commonTitleBar = (CommonTitleBar) findViewById(R.id.common_titlebar);
		commonTitleBar.setCenterText(getString(R.string.my_message));
		commonTitleBar.setReturnVisible();
		switch_message_tongzhi=(ToggleButton)findViewById(R.id.switch_message_tongzhi);
		ll_message_tongzhi=(LinearLayout)findViewById(R.id.ll_message_tongzhi);
		switch_receive_alarm_clock=(ToggleButton)findViewById(R.id.switch_receive_alarm_clock);
		ll_receive_alarm_clock=(LinearLayout)findViewById(R.id.ll_receive_alarm_clock);

		switchbutton_shake=(ToggleButton)findViewById(R.id.switchbutton_shake);
		switchbutton_receive_shake=(ToggleButton)findViewById(R.id.switchbutton_receive_shake);
		rl_remindtime=(RelativeLayout)findViewById(R.id.rl_remindtime);
		tv_remindtime=(TextView)findViewById(R.id.tv_remindtime);
		rl_sound=(RelativeLayout)findViewById(R.id.rl_sound);
		tv_remind_sound=(TextView)findViewById(R.id.tv_remind_sound);
		alarm_msg_hint=(TextView)findViewById(R.id.alarm_msg_hint);
		impleIRemindTimeable();
		impleIRemindSoundable();
	}

	public void widgetClick() {
		commonTitleBar.setToolBarReturnOnclickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		commonTitleBar.setToolBarReturnOnclickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		switch_message_tongzhi.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				// TODO Auto-generated method stub
				if(isChecked){
					//显示
					ll_message_tongzhi.setVisibility(View.VISIBLE);
				}else {
					ll_message_tongzhi.setVisibility(View.GONE);
				}
			}
		});
		/*switch_message_tongzhi.setOnClickListener(new OnClickListener() {

			@Override
			public void onChange(SwitchButton sb, boolean state) {
				// TODO Auto-generated method stub
				if(state){
					//显示
					ll_message_tongzhi.setVisibility(View.VISIBLE);
				}else {
					ll_message_tongzhi.setVisibility(View.GONE);
				}
			}

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

			}
		});*/
		switch_receive_alarm_clock.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				// TODO Auto-generated method stub
				if(isChecked){
					//显示
					ll_receive_alarm_clock.setVisibility(View.VISIBLE);
					alarm_msg_hint.setVisibility(View.VISIBLE);
				}else {
					ll_receive_alarm_clock.setVisibility(View.GONE);
					alarm_msg_hint.setVisibility(View.GONE);
				}
			}
		});
		/*switch_receive_alarm_clock.setOnChangeListener(new OnChangeListener() {

			@Override
			public void onChange(SwitchButton sb, boolean state) {
				// TODO Auto-generated method stub
				if(state){
					//显示
					ll_receive_alarm_clock.setVisibility(View.VISIBLE);
				}else {
					ll_receive_alarm_clock.setVisibility(View.GONE);
				}
			}
		});*/
		switchbutton_receive_shake.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				// TODO Auto-generated method stub
				if(isChecked){
					//开启振动
					openShock();
					//					AppConstant.isOpenShock = true;
				}else {
					//关闭振动
					stopShock();
					//					AppConstant.isOpenShock = false;
				}
			}
		});
		/*switchbutton_receive_shake.setOnChangeListener(new OnChangeListener() {

			@Override
			public void onChange(SwitchButton sb, boolean state) {
				// TODO Auto-generated method stub
				//				mSharedPreferencesUtil.putIsShock(state);
				if(state){
					//开启振动
					openShock();
					//					AppConstant.isOpenShock = true;
				}else {
					//关闭振动
					stopShock();
					//					AppConstant.isOpenShock = false;
				}
			}
		});*/
		switchbutton_shake.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				// TODO Auto-generated method stub
				if(isChecked){
					//开启振动
					openShock();
					//					AppConstant.isOpenShock = true;
				}else {
					//关闭振动
					stopShock();
					//					AppConstant.isOpenShock = false;
				}
			}
		});
		/*switchbutton_shake.setOnChangeListener(new OnChangeListener() {

			@Override
			public void onChange(SwitchButton sb, boolean state) {
				// TODO Auto-generated method stub
				//				mSharedPreferencesUtil.putIsShock(state);
				if(state){
					//开启振动
					openShock();
					//					AppConstant.isOpenShock = true;
				}else {
					//关闭振动
					stopShock();
					//					AppConstant.isOpenShock = false;
				}
			}
		});*/
		rl_remindtime.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				MyRemindTimeDialog dialog=new MyRemindTimeDialog(mAct,pos);
				dialog.init();
			}
		});
		rl_sound.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				mIntent=new Intent(mAct,MyRingActivity.class);
				startActivity(mIntent);
			}
		});
	}

	private void openShock(){
		/* 
		 * 想设置震动大小可以通过改变pattern来设定，如果开启时间太短，震动效果可能感觉不到 
		 * */  
		long [] pattern = {100,400,100,400};   // 停止 开启 停止 开启   
		vibrator.vibrate(pattern,-1);           //重复两次上面的pattern 如果只想震动一次，index设为-1   
	}

	private void stopShock(){
		vibrator.cancel();
	}

	@Override
	public void initData() {
		commonTitleBar.setCenterText(getString(R.string.my_message));
		commonTitleBar.setReturnVisible();
		ringSound=sp.getString("ringname", getString(R.string.mes_default));

		tv_remind_sound.setText(ringSound);
		if(SharedPreferencesUtil.getInstance(mAct).getIsreceivealarmclock())//表示接收提醒，所以设置check是true
		{
			switch_receive_alarm_clock.setChecked(true);
			String remintimeString=SharedPreferencesUtil.getInstance(mAct).getRemindtime();
			if(!TextUtils.isEmpty(remintimeString)){
				tv_remindtime.setText(remintimeString);
				if(remintimeString.contains("10"))
				{
					pos=2;
				}else if(remintimeString.contains("15")){
					pos=1;
				}else{
					pos=0;
				}
			}
		
			tv_remind_sound.setText(SharedPreferencesUtil.getInstance(mAct).getRemindbell());
			if(SharedPreferencesUtil.getInstance(mAct).getRemindvibration())//true，代表开启震动。
			{
				switchbutton_receive_shake.setChecked(true);
			}else{
				switchbutton_receive_shake.setChecked(false);
			}
		}else{
			//不接收，设置为false，把布局隐藏
			switch_receive_alarm_clock.setChecked(false);
			ll_receive_alarm_clock.setVisibility(View.GONE);
			alarm_msg_hint.setVisibility(View.GONE);
		}
	}


	@Override
	protected void initView() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void setListener() {
		// TODO Auto-generated method stub

	}
	/**
	 * 此方法是实现接口，等到dilaog弹出选中了提醒时间的时候在这里更改内容 回调
	 */
	public void impleIRemindTimeable(){
		iRemindTimeable=new IRemindTimeable() {

			@Override
			public void setRemindTime(String str,int position) {
				// TODO Auto-generated method stub
				tv_remindtime.setText(str);
				pos=position;
			}
		};
	}
	/**
	 * 此方法是实现接口，等到dilaog弹出选中了提醒时间的时候在这里更改内容 回调
	 */
	public void impleIRemindSoundable(){
		iRemindSoundable=new IRemindSoundable() {

			@Override
			public void setRemindSound(String str) {
				// TODO Auto-generated method stub
				tv_remind_sound.setText(str);
			}
		};
	}
	public interface IRemindTimeable{
		void setRemindTime(String str,int pos);
	}
	public interface IRemindSoundable{
		void setRemindSound(String str);
	}
	//在这个回调方法里面，做一些 保存的工作。例如按了返回键，或者后退键，或者home键都会保存此方法的。
	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		boolean preIsAlarmClock=SharedPreferencesUtil.getInstance(mAct).getIsreceivealarmclock();
		boolean nowIsAlarmClock=switch_receive_alarm_clock.isChecked();
		if(nowIsAlarmClock){
			SharedPreferencesUtil.getInstance(mAct).putIsreceivealarmclock(true);
		}else{
			SharedPreferencesUtil.getInstance(mAct).putIsreceivealarmclock(false);
		}
		SharedPreferencesUtil.getInstance(mAct).putRemindtime(tv_remindtime.getText().toString());
		//不需要保存了，因为在铃声确定的时候放到了sharedPre...里面了。
		//		SharedPreferencesUtil.getInstance(mAct).putRemindbell(tv_remind_sound.getText().toString());
		if(switchbutton_receive_shake.isChecked()){
			SharedPreferencesUtil.getInstance(mAct).putRemindvibration(true);
		}else{
			SharedPreferencesUtil.getInstance(mAct).putRemindvibration(false);
		}
		if(nowIsAlarmClock==true&&preIsAlarmClock==false)//如果以前是不用提醒，现在是提醒，那就做异步操作。
		{
			new GetYuyueListAsyncTask(mAct, SharedPreferencesUtil.getInstance(mAct).getUserId()).execute("");
		}else if(nowIsAlarmClock==false&&preIsAlarmClock==true){//如果现在是不需要，以前是需要提醒的，那就取消闹钟。
			//		TODO 取消闹钟
			Intent intent = new Intent(mAct,
					OneShotAlarm.class);
			PendingIntent sender = PendingIntent.getBroadcast(
					mAct, 0, intent, 0);
			AlarmManager am = (AlarmManager) mAct
					.getSystemService(Context.ALARM_SERVICE);
			am.cancel(sender);
		}
	}
}
